package com.cz.nongyetongv5.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.EntDetailActivity;
import com.cz.nongyetongv5.activity.EntInfoProductDetailActivity;
import com.cz.nongyetongv5.bean.EntProduct;


/**
 * Created by zhangliang on 2017/6/26.
 */

public class EntInfoProductFragment extends Fragment {

    private RecyclerView recyclerView;
    private ProductAdapter adapter;
    private View  view;


    public static EntInfoProductFragment newInstance() {
        EntInfoProductFragment fragment = new EntInfoProductFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_ent_product, null);
        recyclerView=view.findViewById(R.id.recyclerView);
        init();
        return view;
    }



    public void init() {
        adapter = new ProductAdapter();
        View notDataView = getActivity().getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(notDataView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), EntInfoProductDetailActivity.class);
                intent.putExtra("entProduct", ((EntDetailActivity) getActivity()).getEntMainProducts().get(position));
                startActivity(intent);
            }
        });

    }


    public class ProductAdapter extends BaseQuickAdapter<EntProduct, BaseViewHolder> {
        public ProductAdapter() {
            super(R.layout.ent_product_item, ((EntDetailActivity) getActivity()).getEntMainProducts());
        }

        @Override
        protected void convert(BaseViewHolder viewHolder, EntProduct item) {
            viewHolder.setText(R.id.tv_product_name, item.getName());
            if (item.getScale_amount() == null || item.getScale_amount().isEmpty()) {
                viewHolder.setText(R.id.tv_product_detail, item.getOutput_amount() + item.getOutput_group_name());
            } else {
                viewHolder.setText(R.id.tv_product_detail, item.getScale_amount() + item.getScale_group_name() +
                        "(" + item.getOutput_amount() + item.getOutput_group_name() + ")");
            }
        }
    }

}